<?php

namespace app\admin\controller;
//验证类
use app\admin\controller\Auth;
use think\Request;

class Upload extends Auth
{
    //需验证方法集
    protected $is_check_login = ['*'];

    //图片上传方法
    public function upImgLoad()
    {
        $post = input('post.');
        if (input('?post.type') && $post['type'] == "logo") {
            $path = ROOT_PATH . 'public' . DS . 'static/';
            $filename = "logo.png";
            $url = $this->uploadImg($path, $filename, "uplogo");
            if ($url['code'] == "0") {
                return request_tips(400, $url['msg']);
            } else {
                return request_tips(200, "上传成功", ['imgUrl' => "/static/" . $url['url']]);
            }
        }
    }

    //图片上传
    public function uploadImg($path, $filename, $name = "file", $type = ["jpg", "jpeg", "png", "gif"])
    {
        //初始化变量
        $data = [
            "code" => "0",
            "msg" => "上传成功",
            "url" => null
        ];
        $type = implode(",", $type);
        //获取表单上传文件
        $file = request()->file($name);
        // 移动到指定目录下
        $res = $file->validate(['size' => 1048576, 'ext' => $type])->move($path, $filename);
        //判断上传结果
        if ($res) {
            $data['code'] = 1;
            // 成功上传后 获取上传信息
            $data['url'] = $res->getFilename();
        } else {
            // 上传失败获取错误信息
            $data['msg'] = $file->getError();
        }
        return $data;
    }
}
